<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Eton extends Model
{
    protected  $table = 'eton_data';

    public function getEtonTotalByUid($userId)
    {
        $etonReport = $this ->getEtonReportByUid($userId);
        if ($etonReport)
        {
            $total = $etonReport['body'];
            $total = explode(',',$total);
            $result['score'] = array_shift($total);
            $result['weight'] = $etonReport['weight'] ?: false;
            return $result;
        }else
        {
            return false;
        }

    }

    /**
     * 获得医顿大项检测分数
     * @author shuoshu
     * @DateTime 2018-08-10T09:25:11+0800
     * @param    [type]                   $uid [description]
     * @return   [type]                        [description]
     */
    protected function getEtonReportByUid($uid)
    {
        $map['eton_report.user_id'] = $uid;
        $map['o.status'] = config('yfapp.APP_SEARCH.ORDER_STATUS.YES.CODE');  //是否付款
        $info = \DB::table('eton_report')->select('eton_report.*', 'b.weight')
            ->leftjoin('user_bmi as b', 'b.user_id', '=', 'eton_report.user_id')
            ->leftjoin('user_order as o', 'o.id', '=', 'eton_report.order_id')//是否付款
            ->where($map)
            ->orderBy('eton_report.id', 'desc')
            ->first();
        if (is_null($info) || !$info) {
            return false;
        } else {
            return get_object_vars($info);
        }
    }

}

